package com.tg.app.activity.device.add;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.RelativeLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.appbase.custom.BuildConfig;
import com.appbase.custom.config.TGConfig;
import com.appbase.custom.constant.CommonConstants;
import com.base.BaseActivity;
import com.hjq.permissions.Permission;
import com.tange.module.core.wifi.scan.WiFiScanManager;
import com.tg.app.R;
import com.tg.app.helper.AppHelper;
import com.tg.app.report.DeviceAddReport;
import com.tg.app.report.DeviceAddReportBean;
import com.tg.app.util.LogUtils;
import com.tg.appcommon.android.PermissionUtil;
import com.tange.base.toolkit.PreferenceUtil;
import com.tg.appcommon.android.TGLog;
import com.tg.network.http.TimeZoneUtils;


public class AddChooseActivity extends BaseActivity implements View.OnClickListener {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);//remove title bar  即隐藏标题栏
        setContentView(R.layout.activity_add_choose);
        hideActionBar();
        initView();

        LogUtils.matTrackCustomKVEvent(this,"device_add","AD1");
        DeviceAddReportBean reportBean = new DeviceAddReportBean();
        reportBean.startTime = System.currentTimeMillis();
        reportBean.userID = String.valueOf(PreferenceUtil.getInt(this, CommonConstants.PRE_USER_ID));
        DeviceAddReport.getInstance().setReportBean(reportBean);
        TimeZoneUtils.setUserTimezone();
    }

    @Override
    public boolean shouldSetStatusBarColor() {
        return true;
    }

    @Override
    protected  void initView(){
        RelativeLayout addDoorBell = findViewById(R.id.rel_add_doorbell);
        RelativeLayout wifiAdd = findViewById(R.id.rel_add_wifi);
        RelativeLayout add4gCar = findViewById(R.id.rel_add_4g_car);

        Button back = findViewById(R.id.add_choose_btn_back);
        findViewById(R.id.rel_add_4g_lamp).setOnClickListener(this);
        addDoorBell.setOnClickListener(this);
        wifiAdd.setOnClickListener(this);
        back.setOnClickListener(this);
        add4gCar.setOnClickListener(this);

        // 没有配置行车记录仪模块，或者 行车记录仪-快速添加 以及 行车记录仪-扫码添加 都没有勾选的话，那么隐藏
        if ((!BuildConfig.device_4gCarCamera_addByFast && !BuildConfig.device_4gCarCamera_addByQrCode)){
            add4gCar.setVisibility(View.GONE);
        } else {
            add4gCar.setVisibility(View.VISIBLE);
        }

        if (BuildConfig.device_wifiDoorBell_addByFast) {
            addDoorBell.setVisibility(View.VISIBLE);
        } else {
            addDoorBell.setVisibility(View.GONE);
        }

        if (BuildConfig.device_wifiCamera_addByFast
                || BuildConfig.device_wifiCamera_addByQrCode
                || BuildConfig.device_wifiCamera_addByWired
                || BuildConfig.device_4gCamera_addByQrCode) {
            wifiAdd.setVisibility(View.VISIBLE);
        } else {
            wifiAdd.setVisibility(View.GONE);
        }

        View doorLockView = findViewById(R.id.rel_add_lockbell);
        doorLockView.setOnClickListener(this);
        if (BuildConfig.device_doorLock_addByFast || BuildConfig.device_doorLock_addByQrCode) {
            doorLockView.setVisibility(View.VISIBLE);
        } else {
            doorLockView.setVisibility(View.GONE);
        }

        if (AppHelper.isDuomigeApp()){
            findViewById(R.id.rel_add_4g_lamp).setVisibility(View.GONE);
        }

        if (AppHelper.isDuomigeApp()){
            doorLockView.setVisibility(View.GONE);
        }
    }

    private final static String[] needQrcodeScanPermissions = {
            Permission.ACCESS_FINE_LOCATION,
            Permission.ACCESS_COARSE_LOCATION
    };

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] paramArrayOfInt) {
        super.onRequestPermissionsResult(requestCode, permissions, paramArrayOfInt);
        if (requestCode == AddFragmentTabActivity.PERMISSON_REQUESTCODE) {
            if (!PermissionUtil.verifyPermissions(paramArrayOfInt)) {      //没有授权
                TGLog.d("onRequestPermissionsResult = false");
            } else {
                TGLog.d("onRequestPermissionsResult = true");
            }
        }
    }

    private boolean requestPermission() {
        PermissionUtil permissionUtil = new PermissionUtil(AddChooseActivity.this);
        return permissionUtil.checkPermissions(AddChooseActivity.this, AddFragmentTabActivity.PERMISSON_REQUESTCODE, AddChooseActivity.needQrcodeScanPermissions);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        WiFiScanManager.scanner().resetDeviceNetWork(this);
    }

    @Override

    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.rel_add_4g_car) {
            Intent carIntent = new Intent(this, Add4GChooseActivity.class);
            startActivity(carIntent);
        } else if (id == R.id.rel_add_wifi) {
            Intent toAp = new Intent(this, AddWifiChooseActivity.class);
            startActivity(toAp);
        } else if (id == R.id.add_choose_btn_back) {
            this.finish();
        }
    }
}
